package com.jzr.pda.controller.api;

import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.pda.db1.model.TPdaPageData;
import com.jzr.pda.db1.service.TPdaPageService;
import com.jzr.pda.db1.vo.PdaPageDataVo;
import com.jzr.pda.db1.vo.PdaPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Api(tags = "PDA页面接口")
@RequestMapping("/api/pdaPage")
public class PdaPageController {
    @Autowired
    private TPdaPageService pdaPageService;

    @ApiOperation("获取Pda页面设置")
    @GetMapping("/getPage")
    public Result<PdaPageVo> getPage(@ApiParam("页面编码") @RequestParam String code,
                                     @ApiParam("数据ID") @RequestParam(required = false) Integer dataId) {
        //System.out.println("dataId:" + dataId);
        try {
            PdaPageVo pdaPageVo = pdaPageService.getPage(code, dataId);
            if(pdaPageVo == null){
                return Result.fail("没有数据");
            }
            return Result.success("成功", pdaPageVo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return Result.fail("获取defaultValue失败");
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
            return Result.fail("获取defaultValue失败,可能对于的字段不存在");
        }catch (InvocationTargetException e) {
            e.printStackTrace();
            return Result.fail("获取defaultValue失败");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("获取数据失败");
        }
    }

    @ApiOperation("获取Pda页面数据列表")
    @GetMapping("/listData")
    public Result<List<PdaPageDataVo>> listData(@ApiParam("病人编码") @RequestParam String patientCode,
                                                @ApiParam("住院号") @RequestParam String patientCureNo) {
        List<PdaPageDataVo> list = pdaPageService.listData(patientCode,patientCureNo);
        return Result.success("成功",list,list.size());
    }

    @ApiOperation("保存Pda页面数据")
    @PostMapping("/saveData")
    public Result<Integer> saveData(@RequestBody @ApiParam("Pda页面数据") TPdaPageData tPdaPageData){
        if(tPdaPageData == null){
            return Result.fail("数据不能为空");
        }
        if(U.isBlank(tPdaPageData.getPageId())){
            return Result.fail("PageId不能为空");
        }
        if(U.isBlank(tPdaPageData.getPatientCode())){
            return Result.fail("PatientCode不能为空");
        }
        if(U.isBlank(tPdaPageData.getPatientCureNo())){
            return Result.fail("PatientCureNo不能为空");
        }

        if(tPdaPageData.getId() != null) {
            //不允许修改几天前的数据
            TPdaPageData oldData = pdaPageService.getDataById(tPdaPageData.getId());
            if(oldData != null && oldData.getUpdateTime() != null){
                if(oldData.getUpdateTime().isBefore(LocalDateTime.now().minusDays(3))){
                    return Result.fail("不能修改3天前的数据");
                }
            }
            if(tPdaPageData.getIsDel() != null && tPdaPageData.getIsDel() == true){
                return Result.fail("保存不成功，该条记录已经被删了");
            }
        }
        int count = pdaPageService.saveData(tPdaPageData);
        if(count <= 0){
            return Result.fail("保存不成功,保存了0条记录");
        }
        return Result.success("保存成功",count);
    }

    @ApiOperation("删除Pda页面数据")
    @PostMapping("/deleteData")
    public Result<Integer> deleteData(Integer id){
        if(id == null){
            return Result.fail("id不能为空");
        }
        int count = pdaPageService.deleteData(id);
        if(count <= 0){
            return Result.fail("删除失败，删除了0记录");
        }
        return Result.success("删除成功",0);
    }

    @ApiOperation("根据id获取Pda页面数据")
    @GetMapping("/getData")
    public Result<TPdaPageData> getData(Integer id){
        TPdaPageData tPdaPageData = pdaPageService.getDataById(id);
        return Result.success("成功",tPdaPageData);
    }


}
